# -*- coding:utf-8 -*-
import os
import psutil as ps
import platform

try:
    from binaryornot.check import is_binary
except:
    os.system('pip install binaryornot')
    from binaryornot.check import is_binary


data = {} # 存放进程信息
def disk_pafu():
    list_disk_pan = []
    print(list_disk_pan)
    for i in ps.disk_partitions():
        list_disk_pan.append(i.device)
    return list_disk_pan

l_path = disk_pafu()

def disk_capa(path):
    capa = ps.disk_usage(path)
    user_par = capa.used / capa.total
    return (user_par, capa.total, capa.free)


def all_disk_user():
    par_list = {}
    for i in l_path:
        num, zong , userd= disk_capa(i)
        num_str = str(num * 100)
        par_list[i] = [num_str[0:num_str.find('.')] +'%', zong // 1024 ** 3, userd // 1024 ** 3]
    return par_list


def get_process_list():
    '''得到进程'''
    for process in ps.process_iter():
        if process is not None:
            pname = process.name()
            status = process.status()
            pid = str(process.pid)
            cpu = round(process.cpu_percent() / 10, 1)
            data[pid]=[pname, status, cpu] 

    del data['0']
    return data 

# 判断文件中是否包含关键字，是则将文件路径打印出来
def is_file_contain_word(file_list, query_word):
    lis = []
    for _file in file_list:
        if query_word in open(_file, encoding='utf-8').read():
            lis.appent(_file)
    return lis
 

def get_all_file_list(floder_path): 
    file_nottwo_path = []
    for dirpath, dirname, filenames in os.walk(floder_path):
        for fne in filenames:
            pa = dirpath + '\\' + fne
            if not (is_binary(pa)):
                file_nottwo_path.append(pa)
    return file_nottwo_path

file_folder_path = []
# 返回指定目录的所有文件和文件夹（包含子目录的文件）
def get_all_file(floder_path, fname):
    
    if floder_path is None:
        raise Exception("floder_path is None")
    for dirpath, dirnames, filenames in os.walk(floder_path):
        filenamess = [i[0:i.find('.')] for i in filenames]

        if fname in filenamess:
            p = dirpath + '\\' + filenames[filenamess.index(fname)]
            if len(p) > 60:
                p = p[0:60] + '...'
            file_folder_path.append(p)
        if fname in dirnames:
            p = dirpath + '\\' + fname
            if len(p) > 60:
                p = p[0:60] + '...'
            file_folder_path.append(p)


def main_file(string):
    file_folder_path.clear()
    for p in l_path:
        get_all_file(p, string)
    return file_folder_path

def test():
    dict_sys = {
        '操作系统名称及版本号': platform.platform(),
        '操作系统版本号': platform.version(),
        '操作系统的位数': platform.architecture(),
        '计算机类型': platform.machine(),
        '计算机的网络名称': platform.node(),
        '计算机处理器信息': platform.processor(),
        '操作系统类型': platform.system()
    }

    return dict_sys


#  cpu
pc_mem = ps.virtual_memory()
def MainRun():
    a = float(ps.cpu_percent())
    return a
def MainRun1():
    b = float(pc_mem.percent)
    return b
